package com.cs.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cs.entity.Result;
import com.cs.mapper.CourseMapper;
import com.cs.pojo.Course;
import com.cs.pojo.CourseExample;
import com.cs.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(timeout = 600000)
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    /***
     * @description 获得所有的课程
     *
     * @param
     * @date 2019/6/20 22:02
     * @return java.util.List<com.cs.pojo.Course>
     */
    @Override
    public List<Course> getCourse() {
        return courseMapper.selectByExample(null);
    }

    /***
     * @description 新增课程
     *
     * @param course
     * @date 2019/6/20 22:04
     * @return com.cs.entity.Result
     */
    @Override
    public Result addCourse(Course course) {
        Result result = null;
        if (course != null) {
            course.setSelected(0);
            courseMapper.insert(course);
            result = new Result(true, "添加成功", course);
        } else {
            result = new Result(false, "课程不能为空");
        }
        return result;
    }


    /***
     * @description 删除课程
     *
     * @param id
     * @date 2019/6/20 22:04
     * @return com.cs.entity.Result
     */
    @Override
    public Result deleteCourse(Integer[] id) {
        Result result = null;
        if (id != null || id.length > 0) {
            for (int i = 0; i < id.length; i++) {
                courseMapper.deleteByPrimaryKey(id[i]);
            }
            result = new Result(true, "删除成功");
        } else {
            result = new Result(false, "删除失败");
        }
        return result;
    }


    /***
     * @description 修改课程
     *
     * @param course
     * @date 2019/6/20 22:05
     * @return com.cs.entity.Result
     */
    @Override
    public Result updateCourse(Course course) {
        Result result = null;
        if (course != null) {
            courseMapper.updateByPrimaryKeySelective(course);
            result = new Result(true, "修改成功");
        } else {
            result = new Result(false, "修改失败");
        }
        return result;
    }

    /***
     * @description 根据id查找课程
     *
     * @param id
     * @date 2019/6/20 22:05
     */
    @Override
    public Course findCourseById(Integer id) {
        return courseMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Course> findCourseFuzzy(Course course) {
        List<Course> courses = null;
        if (course != null) {
            CourseExample courseExample = new CourseExample();
            courseExample.createCriteria().andNameLike("%" + course.getName() + "%");
            courses = courseMapper.selectByExample(courseExample);
        }
        return courses;
    }
}
